package com.itheima.myiotest4;

import java.io.*;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) throws IOException {

        //先获取本地输错次数记录
        String s1 = getString("myiotest\\src\\com\\itheima\\myiotest4\\count.txt");
        int count = Integer.parseInt(s1);

        //判断输错次数
        if (count > 3) {
            System.out.println("登录次数过多，请明天再试");
            System.exit(0);
        }

        //获取本地文件的用户账号信息
        String s = getString("myiotest\\src\\com\\itheima\\myiotest4\\userinfo.txt");
        System.out.println(s);

        //获取用户输入的账号和密码
        String[] split = s.split("&");
        String username = split[0].split("=")[1];
        String password = split[1].split("=")[1];

        //用户输入
        System.out.println("请输入账号和密码");
        Scanner sc = new Scanner(System.in);
        String username1 = sc.nextLine();
        String password1 = sc.nextLine();

        //判断
        if (username.equals(username1) && password.equals(password1)) {
            System.out.println("登录成功");
            //输入正确，将连续输错次数归零
            writerCount(1);
        } else {
            System.out.println("登录失败");
            System.out.println("第" + count + "次输错,还有"+ (3 - count) + "次机会");
            //并修改次数记录
            writerCount(++count);
        }
    }

    private static String getString(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String s = br.readLine();
        br.close();
        return s;
    }

    private static void writerCount(int count) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("myiotest\\src\\com\\itheima\\myiotest4\\count.txt"));
        bw.write(count + "");
        bw.close();
    }
}
